package com.dongdong.service.impl;

import com.dongdong.mapper.TbSpecificationMapper;
import com.dongdong.mapper.TbSpecificationOptionMapper;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbSpecificationOption;
import com.dongdong.pojo.TbSpecificationOptionExample;
import com.dongdong.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<TbSpecification> select(String specName) {
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectName(specName);
        return tbSpecifications;
    }



    @Override
    public boolean insertAll(List<TbSpecificationOption> list, String name) {
        try {
            TbSpecification tbSpecification = new TbSpecification();
            tbSpecification.setSpecName(name);
//            增加规格表
            tbSpecificationMapper.insertName(tbSpecification);
//            增加中间表
            tbSpecificationOptionMapper.insertAll(list,tbSpecification.getId());
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean delete(Long id) {
        try {
//        删除规格表
            tbSpecificationMapper.deleteByPrimaryKey(id);
//        删除中间表
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(id);
            tbSpecificationOptionMapper.deleteByExample(example);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public TbSpecification toupdate(Long id) {
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        return tbSpecification;
    }

    @Override
    public  List<TbSpecificationOption> toupte(Long id) {
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> list = tbSpecificationOptionMapper.selectByExample(example);
        return list;
    }

    @Override
    public boolean update(List<TbSpecificationOption> list, TbSpecification tbSpecification) {
        try {
//            修改规格表
            tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);

//            先根据当前id删除中间表的全部
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(tbSpecification.getId());
            tbSpecificationOptionMapper.deleteByExample(example);

//            在增加中间表
            tbSpecificationOptionMapper.insertAll(list,tbSpecification.getId());
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public List<TbSpecification> selectAll() {
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(null);
        return tbSpecifications;
    }
}
